package com.es.config;

import com.es.common.ElasticsearchUtil;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ES配置类
 * author: 阿杰
 */
@Configuration
public class ElasticSearchClientConfig {

    /**
     * ES 地址
     */
    @Value("${es.ip}")
    private String hostName;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        String[] points = hostName.split(",");
        HttpHost[] httpHosts = new HttpHost[points.length];
        for (int i = 0; i < points.length; i++) {
            String point = points[i];
            httpHosts[i] = new HttpHost(point.split(":")[0], Integer.parseInt(point.split(":")[1]), "http");
            // httpHosts[i] = HttpHost.create(point);
        }
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(httpHosts));
        return client;
    }

    @Bean
    public ElasticsearchUtil elasticSearchUtil() {
        return new ElasticsearchUtil();
    }


}
